package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemImage3Binding;
import com.example.yichuangyun.utils.pictrue.GlideEngine;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;

import java.util.ArrayList;

public class ImageAdapter extends BaseQuickAdapter<LocalMedia, DataBindingHolder<ItemImage3Binding>> {

    private final Context context;

    public ImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemImage3Binding> itemImageBindingDataBindingHolder, int i, @Nullable LocalMedia localMedia) {
        ItemImage3Binding binding = itemImageBindingDataBindingHolder.getBinding();
        if (localMedia != null) {
            Glide.with(context)
                    .load(localMedia.getPath())
                    .centerCrop()
                    .placeholder(R.color.color_F6F6F6)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(binding.ivImage);
            itemImageBindingDataBindingHolder.itemView.setOnClickListener(view -> PictureSelector.create(context)
                    .openPreview()
                    .setImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                    .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                        @Override
                        public void onPreviewDelete(int position) {

                        }

                        @Override
                        public boolean onLongPressDownload(Context context, LocalMedia media) {  // 长按保存图片
                            return false;
                        }
                    })
                    .startActivityPreview(itemImageBindingDataBindingHolder.getAbsoluteAdapterPosition(), false, (ArrayList<LocalMedia>) getItems()));
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemImage3Binding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_image3, viewGroup);
    }
}
